import {defineStore} from 'pinia'
import {ref} from 'vue'
import type {LoginParams, UserInfo} from '@/api/auth'
import {loginApi} from '@/api/auth'

export const useAuthStore = defineStore('auth', {
    state: () => ({
        token: ref<string | null>(null),
        userInfo: ref<UserInfo | null>(null),
        isAuthenticated: ref(false),
    }),

    actions: {
        async login(params: LoginParams) {
            const res = await loginApi(params)
            this.token = res.token
            this.isAuthenticated = true
        },
        logout() {
            this.token = null
            this.userInfo = null
            this.isAuthenticated = false
        }
    },

    persist: true, // 启用持久化
})